package com.vf.admin.client.service.impl;

import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dao.AppUserDao;
import com.vf.admin.client.dao.AppVisitorDao;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.AppVisitorService;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.entity.AppVisitor;
import com.vf.admin.utils.AgeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import static com.vf.admin.common.Constants.guizuCacheMap;

/**
 * 访客记录
 */
@Service
public class AppVisitorServiceImpl implements AppVisitorService {

    @Autowired
    AppVisitorDao appVisitorDao;
    @Autowired
    AppUserDao appUserDao;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AppUserService appUserService;

    @Override
    public List<AppVisitor> list(Integer type, Map<String, Object> paraMap, SplitPageBean splitPage) {
        AppUser user = null;
        AppUser gustUser = null;
        List<AppVisitor> list = appVisitorDao.list(paraMap, splitPage);
        if (type == 1) {
            for (AppVisitor visitor : list) {
                user = appUserService.get(visitor.getUserid());
                gustUser = appUserService.get(visitor.getGustid());
                if (gustUser != null && user != null) {
                    if (gustUser.getBirthday() != null) {
                        gustUser.setAge(AgeUtils.getAgeByBirth(gustUser.getBirthday()));
                    }

                    visitor.setSign(gustUser.getMysign());

                    Map<String, Object> vipMap = appUserService.isVipMap(visitor.getGustid());
                    gustUser.setVipMap(vipMap);
                    visitor.setUser(gustUser);

                    if (redisUtil.hasKey(RedisKeys.BECKONING + (Integer) paraMap.get("userId") + "-" + gustUser.getId())) {
                        visitor.setIsBeckoning(1);
                    } else {
                        visitor.setIsBeckoning(2);
                    }

                    if (user.getIsguizu() == 1) {
                        //是贵族
                        //根据贵族id  获取贵族图标
                        String medal = guizuCacheMap.get(user.getNobleid());
                        visitor.setMedal(medal);
                    } else {
                        visitor.setMedal("");
                    }

                    if (gustUser.getIsguizu() == 1) {
                        //是贵族
                        //根据贵族id  获取贵族图标
                        String medal = guizuCacheMap.get(gustUser.getNobleid());
                        visitor.setGustMedal(medal);
                    } else {
                        visitor.setGustMedal("");
                    }

                    if (user.getVipEndDate() != null) {
                        visitor.setIsVip(1);
                    }
                    if (gustUser.getVipEndDate() != null) {
                        visitor.setGustIsVip(1);
                    }
                }
            }
        } else if (type == 2) {
            for (AppVisitor visitor : list) {
                user = appUserService.get(visitor.getUserid());
                gustUser = appUserService.get(visitor.getGustid());
                if (gustUser != null && user != null) {
                    if (user.getBirthday() != null) {
                        user.setAge(AgeUtils.getAgeByBirth(user.getBirthday()));
                    }

                    visitor.setSign(user.getMysign());

                    Map<String, Object> vipMap = appUserService.isVipMap(visitor.getUserid());
                    user.setVipMap(vipMap);
                    visitor.setUser(user);

                    if (redisUtil.hasKey(RedisKeys.BECKONING + (Integer) paraMap.get("userId") + "-" + user.getId())) {
                        visitor.setIsBeckoning(1);
                    } else {
                        visitor.setIsBeckoning(2);
                    }

                    if (user.getIsguizu() == 1) {
                        String medal = guizuCacheMap.get(user.getNobleid());
                        visitor.setMedal(medal);
                    } else {
                        visitor.setMedal("");
                    }
                    if (gustUser.getIsguizu() == 1) {
                        String medal = guizuCacheMap.get(gustUser.getNobleid());
                        visitor.setGustMedal(medal);
                    } else {
                        visitor.setGustMedal("");
                    }
                    if (user.getVipEndDate() != null) {
                        visitor.setIsVip(1);
                    }
                    if (gustUser.getVipEndDate() != null) {
                        visitor.setGustIsVip(1);
                    }
                }
            }
        }
        return list;
    }

    @Override
    public AppVisitor get(Integer id) {
        return appVisitorDao.get(id);
    }

    @Override
    public void save(AppVisitor haoYou) {
        appVisitorDao.save(haoYou);
    }

    @Override
    public void del(Integer id) {
        appVisitorDao.del(id);
    }

    @Override
    public int visitorCount(Integer userId) {
        return appVisitorDao.visitorCount(userId);
    }

    @Override
    public AppVisitor findByVisitorLog(Integer from, Integer to) {
        return appVisitorDao.findByVisitorLog(from, to);
    }

}
